#============================================
# #知识点
# 线程得join方法与进程得join用法相同
#============================================
#线程之间得数据可以共享
# from threading import Thread
# import threading
# money = 100

# def task():
#     global money
#     money = 200
#     print(money)

# if __name__ == "__main__":
#     print("线程之前",money)
#     x = Thread(target=task)
#     x.start()
#     x.join()
#     print("线程之后",money)
#============================================
#线程得其他方法
# from threading import Thread,active_count,current_thread
# import time,os
# def task():
#     print("子线程号",os.getpid())
#     print(current_thread().name)
#     time.sleep(1)


# if __name__ == "__main__":
#     x1 = Thread(target=task)
#     x1.start()
#     print("正在活跃得线程",active_count())
#     x1.join()
#     print(current_thread().name)
#     print("主",os.getpid())
#============================================
# 主线程运行结束之后不会立刻结束会等待所有非守护子线程结束才会结束
# 因为主线程的结束意味着所在的进程的结束
# from threading import Thread
# import time
# def task1():
#     print(123)
#     time.sleep(1)
#     print("123end")

# def task2():
#     print(456)
#     time.sleep(3)
#     print("456end")

# if __name__ == "__main__":
#     x1 = Thread(target=task1)
#     x2 = Thread(target=task2)
#     x2.daemon = True
#     x1.daemon = True
#     x1.start()
#     x2.start()
#     print("主")
#============================================
# 互斥锁
from threading import Thread,Lock
import time 
piao = 100

def task():
    global piao
    locks.acquire()
    tmp = piao
    time.sleep(0.1)
    piao = tmp -1
    locks.release()
    # print(piao)
    

if __name__ == "__main__":
    locks = Lock()
    obj_list = []
    for i in range(100):
        x = Thread(target=task)
        x.start()
        obj_list.append(x)
    for i in obj_list:
        i.join()
    print(piao)
#============================================   